﻿@page "/"
@implements IDisposable
@inject XiaoYi_AgentService XiaoYiAgent
<table width="100%" style="text-align:center;">
    <tr>
        <td style="height:10px;"></td>
    </tr>
    <tr>
        <td style="padding:5px;"><Input Placeholder="DeviceId" @bind-Value="@Global.DeivceId" /></td>
    </tr>
    <tr>
        <td style="padding:5px;"><Input Placeholder="OTAUrl" @bind-Value="@Global.OTAUrl" /></td>
    </tr>
    <tr>
        <td style="padding:5px;"><Input Placeholder="WebSocketUrl" @bind-Value="@Global.WebSocketUrl" /></td>
    </tr>
    <tr>
        <td style="padding:5px;"><Input Placeholder="WebSocketToken" @bind-Value="@Global.WebSocketToken" /></td>
    </tr>
    <tr>
        <td style="padding:5px;"><Button Type="ButtonType.Primary" OnClick='()=>{ XiaoYiAgent.Resatrt(); }' style="width:100%;">应用</Button></td>
    </tr>
    <tr>
        <td style="height:10px;"></td>
    </tr>
</table>

<table width="100%" style="text-align:center;">
    <tr>
        <td style="height:10px;"></td>
    </tr>
    <tr>
        <td style="padding:10px;"><h5>@XiaoYiAgent.AnswerMessae</h5></td>
    </tr>
    <tr>
        <td style="padding:5px;"><span style="zoom:2.0;">@XiaoYiAgent.EmotionText</span></td>
    </tr>
    <tr>
        <td style="padding:10px;"><h5>@XiaoYiAgent.QuestionMessae</h5></td>
    </tr>
    <tr>
        <td style="padding:10px;">语音<Switch @bind-Value="XiaoYiAgent.IsAutoRecording" OnChange='()=>{ XiaoYiAgent.AutoRecording(); }' /></td>
    </tr>
    <tr>
        <td style="padding:10px;"><Search Placeholder="" @bind-Value="@txtValue" EnterButton="@("发送")" OnSearch="SendText" OnPressEnter="SendText" Size="InputSize.Large" /></td>
    </tr>
    <tr>
        <td style="padding:10px;"></td>
    </tr>
    <tr>
        <td style="height:100px;">
            @if (!XiaoYiAgent.IsRecording)
            {
                <Button Type="ButtonType.Primary" Shape="ButtonShape.Circle" Size="ButtonSize.Large" Style="zoom:3.0;" OnClick='()=>{ XiaoYiAgent.StartRecording(); }'>录音</Button>
            }
            else
            {
                <Button Type="ButtonType.Primary" Shape="ButtonShape.Circle" Size="ButtonSize.Large" Style="zoom:3.0;" OnClick='()=>{ XiaoYiAgent.StopRecording(); }'>停止</Button>
            }
        </td>
    </tr>
    <tr>
        <td style="height:10px;"></td>
    </tr>
    <tr>
        <td style="padding:20px;"><Button Type="ButtonType.Primary" OnClick='()=>{ XiaoYiAgent.Abort(); }' style="width:100%;">打断</Button></td>
    </tr>
    <tr>
        <td style="height:10px;"></td>
    </tr>
</table>


@code{
    private string txtValue = string.Empty;
    private System.Timers.Timer _timer = new System.Timers.Timer(1000) { Enabled = true };
    protected override async Task OnInitializedAsync()
    {
        _timer.Elapsed += Timer_Elapsed;

        await XiaoYiAgent.SendMessage("你好");
    }
    private async void Timer_Elapsed(object sender, EventArgs args)
    {
        await Task.Factory.StartNew(() =>
        {
            InvokeAsync(() => { StateHasChanged(); return Task.CompletedTask; });
        });
    }
    void IDisposable.Dispose()
    {
        _timer.Close();
    }
    async Task SendText()
    {
        if (!string.IsNullOrEmpty(txtValue))
        {
            await XiaoYiAgent.SendMessage(txtValue);
            txtValue = string.Empty;
        }
    }


}
